package cs236369.webserver.dynamicLoad;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

import org.apache.http.protocol.HttpRequestHandler;

public class HandlerLoader {
	private final Properties args;
	private final Constructor<HttpRequestHandler> con;
	
	@SuppressWarnings("unchecked")
	public HandlerLoader(String fullyQualifiedName, Properties args) throws SecurityException, NoSuchMethodException, ClassNotFoundException {
		super();
		this.args = args;
		this.con = (Constructor<HttpRequestHandler>) Class.forName(fullyQualifiedName).getConstructor(Properties.class);
	}
	
	public synchronized HttpRequestHandler newInstance() throws InstantiationException, IllegalAccessException, InvocationTargetException {
		try {
			return con.newInstance(args);
		} catch (IllegalArgumentException e) {
			return null; // impossible occurence but we must catch it here.
		}
	}
}
